package pt.iscte.pcd.project.remote;

import java.util.LinkedList;

import pt.iscte.pcd.project.exceptions.FileSystemException;
import pt.iscte.pcd.project.interfaces.EntryType;
import pt.iscte.pcd.project.interfaces.PCDDirectory;
import pt.iscte.pcd.project.interfaces.PCDEntry;
import pt.iscte.pcd.project.interfaces.PCDFile;

public class RemoteDirectory extends RemoteEntry implements PCDDirectory {
	private static final long serialVersionUID = 1L;
	private LinkedList<RemoteEntry> listFiles = new LinkedList<RemoteEntry>();
	private int lengthPaste;
	
	public RemoteDirectory(String fullPath, String directoryName, PCDDirectory parent, int lengthPaste) {
		super(fullPath, directoryName, EntryType.DIRECTORY, parent);
		this.setLengthPaste(lengthPaste);

	}

	@Override
	public boolean entryExists(String name) {
		try {
			String[] listFiles = getDirectoryListing();
			for (String string : listFiles) {
				if(string.equals(name)) return true;
			}
		} catch (FileSystemException e) {
			e.printStackTrace();
		}
		return false;
	}

	@Override
	public PCDFile newFile(String name) throws FileSystemException {
		return null;
	}

	@Override
	public PCDDirectory newDirectory(String name) throws FileSystemException {
		return null;
	}

	@Override
	public void delete(PCDEntry entry) throws FileSystemException {}

	@Override
	public String[] getDirectoryListing() throws FileSystemException {
		String[] finalVector = new String[getListFiles().size()];
		for (int i = 0; i < finalVector.length; i++) {
			finalVector[i] = getListFiles().get(i).getName();
		}
		return finalVector;
	}

	@Override
	public PCDEntry getEntry(String name) {
		for (PCDEntry list : getListFiles()) {
			if(list.getName().equals(name))
				return list;
		}
		return null;
	}

	public LinkedList<RemoteEntry> getListFiles() {
		return listFiles;
	}

	public void setListFiles(LinkedList<RemoteEntry> listFiles) {
		this.listFiles = listFiles;
	}

	public int lengthPaste(){
		return getLengthPaste();
	}

	@Override
	public synchronized boolean readLock() {
		return false;
	}

	public int getLengthPaste() {
		return lengthPaste;
	}

	public void setLengthPaste(int lengthPaste) {
		this.lengthPaste = lengthPaste;
	}

}
